package javolution.context;

import javax.realtime.MemoryArea;
import javax.realtime.RealtimeThread;
import javolution.context.ConcurrentContext;
import javolution.lang.Reflection;

/* loaded from: classes2.dex */
class ConcurrentThread extends RealtimeThread {
    private static int _Count;
    private ConcurrentContext.Default _context;
    private volatile Runnable _logic;
    private MemoryArea _memoryArea;
    private String _name = "ConcurrentThread-" + getCount();
    private int _priority;
    private Thread _source;
    private boolean _terminate;
    private static final Reflection.Method SET_NAME = Reflection.getMethod("java.lang.Thread.setName(String)");
    private static final Reflection.Method SET_DAEMON = Reflection.getMethod("java.lang.Thread.setDaemon(boolean)");

    public ConcurrentThread() {
        Reflection.Method method = SET_NAME;
        if (method != null) {
            method.invoke(this, this._name);
        }
        Reflection.Method method2 = SET_DAEMON;
        if (method2 != null) {
            method2.invoke(this, new Boolean(true));
        }
    }

    private synchronized int getCount() {
        int i;
        i = _Count;
        _Count = i + 1;
        return i;
    }

    public boolean execute(Runnable runnable, ConcurrentContext.Default r4) {
        if (this._logic != null) {
            return false;
        }
        synchronized (this) {
            if (this._logic != null) {
                return false;
            }
            this._memoryArea = RealtimeThread.getCurrentMemoryArea();
            this._priority = Thread.currentThread().getPriority();
            this._context = r4;
            this._logic = runnable;
            notify();
            return true;
        }
    }

    @Override // java.lang.Thread, java.lang.Runnable
    public void run() {
        while (true) {
            synchronized (this) {
                while (this._logic == null && !this._terminate) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        throw new ConcurrentException(e);
                    }
                }
            }
            if (this._logic == null) {
                return;
            }
            try {
                Thread currentThread = Thread.currentThread();
                if (currentThread.getPriority() != this._priority) {
                    currentThread.setPriority(this._priority);
                }
                this._context.started();
                this._memoryArea.executeInArea(this._logic);
            } finally {
                try {
                } finally {
                }
            }
        }
    }

    public void terminate() {
        synchronized (this) {
            this._terminate = true;
            notify();
        }
    }

    @Override // java.lang.Thread
    public String toString() {
        return this._name + "(" + this._source + ")";
    }
}
